package com.jd.jr.util;

import com.ibatis.sqlmap.engine.type.BaseTypeHandler;
import com.jd.jr.model.ExtendsVo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


// 继承自BaseTypeHandler<Object> 使用Object是为了让JsonUtil可以处理任意类型
public class JsonTypeHandler extends BaseTypeHandler {
    @Override
    public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType) throws SQLException {
        ps.setString(i, JsonUtil.stringify(parameter));
    }

    @Override
    public ExtendsVo getResult(ResultSet rs, String columnName) throws SQLException {
        return JsonUtil.parse(rs.getString(columnName), ExtendsVo.class);
    }

    @Override
    public ExtendsVo getResult(ResultSet rs, int columnIndex) throws SQLException {
        return JsonUtil.parse(rs.getString(columnIndex), ExtendsVo.class);
    }

    @Override
    public ExtendsVo getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return JsonUtil.parse(cs.getString(columnIndex), ExtendsVo.class);
    }

    @Override
    public ExtendsVo valueOf(String s) {
        return JsonUtil.parse(s,ExtendsVo.class);
    }

   
}